package com.carleasoft.mps.cms.business.article.service.Impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carleasoft.mps.cms.business.article.dao.ArticleAccessoryDao;
import com.carleasoft.mps.cms.business.article.bean.entity.ArticleAccessoryEntity;
import com.carleasoft.mps.cms.business.article.bean.po.ArticlePo;
import com.carleasoft.mps.cms.business.article.service.ArticleAccessoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;


@Service
public class ArticleAccessoryServiceImpl implements ArticleAccessoryService {

    @Autowired
    private ArticleAccessoryDao articleAccessoryDao;

    @Override
    public int insertArticleFile(ArticlePo articlePo) {
        int result = 1;
        if (articlePo.getArticleAccessoryEntityList() != null && !articlePo.getArticleAccessoryEntityList().isEmpty()) {
//            for (ArticleAccessoryEntity accessoryEntity: articlePo.getArticleAccessoryEntityList()) {
//                accessoryEntity.setArticleCode(articlePo.getArticleCode());
//                result = articleAccessoryDao.insert(accessoryEntity);
//            }
            ArticleAccessoryEntity articleAccessoryEntity = new ArticleAccessoryEntity();
            articleAccessoryEntity.setArticleCode(articlePo.getArticleCode());
            articleAccessoryEntity.setFileName(articlePo.getArticleAccessoryEntityList());
            result = articleAccessoryDao.insert(articleAccessoryEntity);

        }
        return result;
    }

    @Override
    public List<ArticleAccessoryEntity> findByCode(String articleCode) {
        LambdaQueryWrapper<ArticleAccessoryEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ArticleAccessoryEntity::getArticleCode, articleCode);
        return articleAccessoryDao.selectList(queryWrapper);
    }

    @Override
    public List<ArticleAccessoryEntity> findByCodeAndFileName(String articleCode, String fileName) {
        LambdaQueryWrapper<ArticleAccessoryEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ArticleAccessoryEntity::getArticleCode, articleCode);
        List<ArticleAccessoryEntity> accessoryEntities = articleAccessoryDao.selectList(queryWrapper);
        Iterator<ArticleAccessoryEntity> iter = accessoryEntities.iterator();
        while (iter.hasNext()){
            boolean contains = false;
            ArticleAccessoryEntity vo = iter.next();
            String file = "{\"employees\":" + vo.getFileName() + "}";
//            JSONObject json = JSONObject.parseObject(file);
            JSONObject jsonObj = new JSONObject(file);
            JSONArray employees = jsonObj.getJSONArray("employees");
            for (int i = 0; i < employees.size(); i++) {
                JSONObject employee = employees.getJSONObject(i);
                String firstName = employee.get("name").toString();
                contains = firstName.contains(fileName);
            }
            if(!contains){
                iter.remove();
            }
        }
        return accessoryEntities;
    }

    @Override
    public int updateFileByCode(ArticlePo articlePo) {
        int result;
        result = articleAccessoryDao.deleteById(articlePo.getArticleCode());
        if (articlePo.getArticleAccessoryEntityList() != null && !articlePo.getArticleAccessoryEntityList().isEmpty()) {
//            for (ArticleAccessoryEntity accessoryEntity : articlePo.getArticleAccessoryEntityList()) {
//                accessoryEntity.setArticleCode(articlePo.getArticleCode());
//                result = articleAccessoryDao.insert(accessoryEntity);
//            }
            ArticleAccessoryEntity articleAccessoryEntity = new ArticleAccessoryEntity();
            articleAccessoryEntity.setArticleCode(articlePo.getArticleCode());
            articleAccessoryEntity.setFileName(articlePo.getArticleAccessoryEntityList());
            result = articleAccessoryDao.insert(articleAccessoryEntity);

        }
        return result;
    }
}
